//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmHestonHullWhiteRatesPart.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
#include <gen/QL/Processes/HullWhiteProcess.h>
#include <gen/QL/Experimental/Finitedifferences/TripleBandLinearOp.h>
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::CFdmHestonHullWhiteRatesPart (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Cephei::QL::Processes::IHullWhiteProcess^ hwProcess) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CFdmMesher^ _Cmesher;
    CHullWhiteProcess^ _ChwProcess;
    try
    {
#ifdef HANDLE
        _phFdmHestonHullWhiteRatesPart = NULL;
#endif
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        _ChwProcess = safe_cast<CHullWhiteProcess^> (hwProcess);
        _ChwProcess->Lock();
        boost::shared_ptr<QuantLib::HullWhiteProcess>& _hwProcess = static_cast<boost::shared_ptr<QuantLib::HullWhiteProcess>&> (_ChwProcess->GetShared ()); 
        _ppFdmHestonHullWhiteRatesPart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteRatesPart> (new QuantLib::FdmHestonHullWhiteRatesPart ( _mesher,  _hwProcess ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_ChwProcess != nullptr) _ChwProcess->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::CFdmHestonHullWhiteRatesPart (boost::shared_ptr<QuantLib::FdmHestonHullWhiteRatesPart>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteRatesPart = NULL;
#endif
	_ppFdmHestonHullWhiteRatesPart = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::CFdmHestonHullWhiteRatesPart (QuantLib::FdmHestonHullWhiteRatesPart& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmHestonHullWhiteRatesPart = NULL;
#endif
	_ppFdmHestonHullWhiteRatesPart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteRatesPart> (&childNative);
    
    _FdmHestonHullWhiteRatesPartOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::CFdmHestonHullWhiteRatesPart (CFdmHestonHullWhiteRatesPart^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteRatesPart = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmHestonHullWhiteRatesPart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteRatesPart> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::CFdmHestonHullWhiteRatesPart (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteRatesPart = NULL;
#endif
	if (!t->IsSubclassOf(CFdmHestonHullWhiteRatesPart::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::CFdmHestonHullWhiteRatesPart (QuantLib::Handle<QuantLib::FdmHestonHullWhiteRatesPart>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonHullWhiteRatesPart = &childNative;
	_ppFdmHestonHullWhiteRatesPart = &static_cast<boost::shared_ptr<QuantLib::FdmHestonHullWhiteRatesPart>>(childNative.currentLink());
    
    _FdmHestonHullWhiteRatesPartOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::CFdmHestonHullWhiteRatesPart (QuantLib::Handle<QuantLib::FdmHestonHullWhiteRatesPart> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonHullWhiteRatesPart = &childNative;
	_ppFdmHestonHullWhiteRatesPart = &static_cast<boost::shared_ptr<QuantLib::FdmHestonHullWhiteRatesPart>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::CFdmHestonHullWhiteRatesPart (QuantLib::FdmHestonHullWhiteRatesPart childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteRatesPart = NULL;
#endif
	_ppFdmHestonHullWhiteRatesPart = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteRatesPart> (new QuantLib::FdmHestonHullWhiteRatesPart (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::~CFdmHestonHullWhiteRatesPart ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmHestonHullWhiteRatesPart != NULL)
    {
	    delete _ppFdmHestonHullWhiteRatesPart;
        _ppFdmHestonHullWhiteRatesPart = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::!CFdmHestonHullWhiteRatesPart ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmHestonHullWhiteRatesPart != NULL)
    {
	    delete _ppFdmHestonHullWhiteRatesPart;
    }
}
QuantLib::FdmHestonHullWhiteRatesPart& Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::GetReference ()
{
    if (_ppFdmHestonHullWhiteRatesPart == NULL) throw gcnew NativeNullException ();
	return **_ppFdmHestonHullWhiteRatesPart;
}
boost::shared_ptr<QuantLib::FdmHestonHullWhiteRatesPart>& Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::GetShared ()
{
    if (_ppFdmHestonHullWhiteRatesPart == NULL) throw gcnew NativeNullException ();
	return *_ppFdmHestonHullWhiteRatesPart;
}
QuantLib::FdmHestonHullWhiteRatesPart* Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::GetPointer ()
{
    if (_ppFdmHestonHullWhiteRatesPart == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmHestonHullWhiteRatesPart;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmHestonHullWhiteRatesPart>& Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::GetHandle ()
{
	if (_phFdmHestonHullWhiteRatesPart == NULL)
	{
		_phFdmHestonHullWhiteRatesPart = new Handle<QuantLib::FdmHestonHullWhiteRatesPart> (*_ppFdmHestonHullWhiteRatesPart);
	}
	return *_phFdmHestonHullWhiteRatesPart;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::HasNative () 
{
	return (_ppFdmHestonHullWhiteRatesPart != NULL);
}

Cephei::QL::Experimental::Finitedifferences::ITripleBandLinearOp^ Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::GetMap::get ()
{
    try
    {
    	QuantLib::TripleBandLinearOp& _rv = (QuantLib::TripleBandLinearOp&)(*_ppFdmHestonHullWhiteRatesPart)->getMap ( );   
        Cephei::QL::Experimental::Finitedifferences::CTripleBandLinearOp^ _nrv = gcnew Cephei::QL::Experimental::Finitedifferences::CTripleBandLinearOp (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Finitedifferences::IFdmHestonHullWhiteRatesPart^ Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart::SetTime (Double t1, Double t2)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Time _t1 = (QuantLib::Time)ValueHelper::Convert (t1);
        QuantLib::Time _t2 = (QuantLib::Time)ValueHelper::Convert (t2);
    	(*_ppFdmHestonHullWhiteRatesPart)->setTime ( _t1,  _t2 );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmHestonHullWhiteRatesPart^ Cephei::QL::Experimental::Finitedifferences::CFdmHestonHullWhiteRatesPart_Factory::Create (Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, Cephei::QL::Processes::IHullWhiteProcess^ hwProcess)
{
    return gcnew CFdmHestonHullWhiteRatesPart ( mesher,  hwProcess);
}
